package com.hengtiansoft.lfy.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import com.hengtiansoft.lfy.R;
import com.hengtiansoft.lfy.base.BaseFragment;

/**
 * 对话 tab fragment
 * daiye
 */
public class DialogueFragment extends BaseFragment implements View.OnClickListener {
    private int index=0;
    private Button[] mTabs;
    // 当前fragment的index
    private int currentTabIndex;
    private Fragment[] fragments;
    private Fragment dialogueTalkFragment;
    private ScenceFragment scenceFtagment;
    private ImageView iv_download_all;
    private ViewPager fragment_container;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_dialogue, container, false);
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
    }

    private void initView() {
        iv_download_all= (ImageView) getView().findViewById(R.id.iv_downLoadAll);
        iv_download_all.setOnClickListener(this);
        dialogueTalkFragment=new DialogueTalkFragment();
        scenceFtagment=new ScenceFragment();
        fragments = new Fragment[] { dialogueTalkFragment, scenceFtagment};
        // 添加显示第一个fragment
       /* getChildFragmentManager().beginTransaction().add(R.id.fragment_container, dialogueTalkFragment)
                .add(R.id.fragment_container, scenceFtagment).hide(scenceFtagment).show(dialogueTalkFragment)
                .commit();*/
        fragment_container= (ViewPager) getView().findViewById(R.id.fragment_container);
        fragment_container.setOffscreenPageLimit(2);

        fragment_container.setAdapter(new MyFragmentAdapter( getChildFragmentManager()));
        fragment_container.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        firstSelected(true);
                        break;
                    case 1:
                        firstSelected(false);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        fragment_container.setCurrentItem(0);
        mTabs = new Button[4];
        mTabs[0] = (Button) getView().findViewById(R.id.btn_dialogue);
        mTabs[1] = (Button) getView().findViewById(R.id.btn_scence);
        mTabs[0].setOnClickListener(this);
        mTabs[1].setOnClickListener(this);
        // 把第一个tab设为选中状态
        mTabs[0].setSelected(true);
    }
    public void firstSelected(boolean istrue){
        mTabs[0].setSelected(istrue);
        mTabs[1].setSelected(!istrue);
        if(istrue) {
            iv_download_all.setVisibility(View.GONE);
        }else{
            iv_download_all.setVisibility(View.VISIBLE);
        }
    }
    /**
     * button点击事件
     *
     * @param view
     */
    public void onTabClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_dialogue:
                iv_download_all.setVisibility(View.GONE);
              //  index = 0;
                fragment_container.setCurrentItem(0);
                break;
            case R.id.btn_scence:
                iv_download_all.setVisibility(View.VISIBLE);
               // index = 1;
                fragment_container.setCurrentItem(1);
                break;
        }
    /*  if (currentTabIndex != index) {
            FragmentTransaction trx = getChildFragmentManager().beginTransaction();
            trx.setCustomAnimations(android.R.anim.slide_in_left,android.R.anim.slide_out_right);
            trx.hide(fragments[currentTabIndex]);
            if (!fragments[index].isAdded()) {
                trx.add(R.id.fragment_container, fragments[index]);
            }
            trx.show(fragments[index]).commit();
        }
        mTabs[currentTabIndex].setSelected(false);
        // 把当前tab设为选中状态
        mTabs[index].setSelected(true);
        currentTabIndex = index;*/
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_dialogue:case R.id.btn_scence:
                onTabClicked(view);
                break;
            case R.id.iv_downLoadAll:
                scenceFtagment.showDownAll();
                break;
        }
    }

    /**
     * 默认进入对话页面
     * @param hidden
     */
    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if(!hidden){
            if(fragment_container != null)
                fragment_container.setCurrentItem(0);
        }
    }

    public class MyFragmentAdapter extends FragmentPagerAdapter {

        public MyFragmentAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
                return fragments[position];
        }

        @Override
        public int getCount() {
            return 2;
        }
    }
}
